package com.zjc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjc.common.R;
import com.zjc.pojo.Student;
import com.zjc.pojo.ViolationRecord;
import com.zjc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周佳成
 * @since 2023-09-17
 */
@RestController
@RequestMapping("/api/vr")
public class ViolationRecordController {
	@Autowired
	ViolationRecordService violationRecordService;

	@Autowired
	StudentService studentService;

	@Autowired
	StuRoomService stuRoomService;

	@Autowired
	BuliderService buliderService;
	@Autowired
	VisitTableService visitTableService;


	@GetMapping("/find")
	public R<Page> GetVrRecordPage(int pageNum, int pageSize, String search){
		QueryWrapper<ViolationRecord> wrapper = new QueryWrapper<>();
        if(search!=null){
			wrapper.like("stu_name",search);
		}
		Page<ViolationRecord> page1 = new Page<>(pageNum,pageSize);
		violationRecordService.page(page1,wrapper);
		return R.success(page1);
	}
	@PutMapping("/update")
	public R<String> changeVr(@RequestBody ViolationRecord vr){
		Integer roomid = vr.getRoomId();
		Integer stuid = vr.getStuId();
		String name = vr.getStuName();
		if(!studentService.isStuExists(stuid)){
			return  R.error("学号不存在！");
		}
		if(!studentService.isRoomExists(roomid)){
			return R.error("房间号不存在！");
		}
		if(!buliderService.isExistBulider(vr.getLdId())){
			return R.error("楼栋不存在！");
		}
		Student student = studentService.getById(stuid);
		if(!vr.getLdId().equals(student.getLdId())){
			return R.error("楼栋号不匹配");
		}
		if(!roomid.equals(student.getRoomId())){
			return R.error("房间号不匹配");
		}
		if(!visitTableService.isMatch4Stuid2name(stuid,name)){
			return R.error("学号姓名不匹配！！");
		}
		boolean rows=violationRecordService.updateById(vr);
		System.out.println(rows);

		return R.success("修改成功");
	}
	@PostMapping("/add")
	public R<String> addVr(@RequestBody ViolationRecord vr){
		Integer roomid = vr.getRoomId();
		Integer stuid = vr.getStuId();
		String name = vr.getStuName();

		if(!studentService.isStuExists(stuid)){
			return  R.error("学号不存在！");
		}
		Student student = studentService.getById(stuid);

		if(!studentService.isRoomExists(roomid)){
			return R.error("房间号不存在！");
		}
		if(!buliderService.isExistBulider(vr.getLdId())){
			return R.error("楼栋不存在！");
		}
		if(!vr.getLdId().equals(student.getLdId())){
			return R.error("楼栋号不匹配");
		}
		if(!roomid.equals(student.getRoomId())){
			return R.error("房间号不匹配");
		}
		if(!visitTableService.isMatch4Stuid2name(stuid,name)){
			return R.error("学号姓名不匹配！！");
		}
		violationRecordService.save(vr);
		return R.success("添加成功");
	}

	@DeleteMapping("/delete/{vrId}")
	public R<String> deleteVr(@PathVariable Integer vrId){
		System.out.println(vrId);
		boolean rows=violationRecordService.deletevr(vrId);
		if(rows){
			return R.success("删除成功");
		}
		else{
			return R.error("删除失败");
		}
	}
}

